package com.rising.face.ali.bishi.ReentrantLock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: rising
 * @Description:
 * 题目：多线程之间按顺序调用，实现A-》B-》C三个线程启动，要求如下：
 * AA打印5次，BB打印10次，CC打印15次
 * 紧接着
 * AA打印5次，BB打印10次，CC打印15次
 * @Date: create in 2021/1/10 11:54
 */
public class ReentrantLockTest {

    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition conditionA = lock.newCondition();
        Condition conditionB = lock.newCondition();
        Condition conditionC = lock.newCondition();
        new Thread(() -> {
            lock.lock();
            try {
                conditionA.await();
                while (true) {
                    //Thread.sleep(500);
                    System.out.print("启动"+Thread.currentThread().getName()+"线程：");
                    for (int i=0; i < 5; i++) {
                        Thread.sleep(100);
                        System.out.print(Thread.currentThread().getName() + "\t");
                    }
                    System.out.println();
                    conditionB.signal();
                    conditionA.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"AA").start();

        new Thread(() -> {
            lock.lock();
            try {
                conditionB.await();
                while (true) {
                    //Thread.sleep(500);
                    System.out.print("启动"+Thread.currentThread().getName()+"线程：");
                    for (int i=0; i < 10; i++) {
                        Thread.sleep(100);
                        System.out.print(Thread.currentThread().getName() + "\t");
                    }
                    System.out.println();
                    conditionC.signal();
                    conditionB.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"BB").start();

        new Thread(() -> {
            lock.lock();
            try {
                conditionC.await();
                while (true) {
                    //Thread.sleep(500);
                    System.out.print("启动"+Thread.currentThread().getName()+"线程：");
                    for (int i=0; i < 15; i++) {
                        Thread.sleep(100);
                        System.out.print(Thread.currentThread().getName() + "\t");
                    }
                    System.out.println();
                    System.out.println("----------------------等待两秒继续下一组----------------------");
                    Thread.sleep(2 * 1000);
                    conditionA.signal();
                    conditionC.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"CC").start();

        Thread.sleep(2 * 1000);
        lock.lock();
        conditionA.signalAll();
        lock.unlock();
    }
}
